package com.xiaomage.handlerdemo

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log

class SonThread2SonThreadActivity : AppCompatActivity() {
    private val TAG="子线程通信"
    private var handler1:Handler?=null
    private var handler2:Handler?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_son_thread_2_son_thread)
        MyThread1().start()
        MyThread2().start()
    }
    inner class MyThread1:Thread(){
        override fun run() {
            Looper.prepare()
            handler1=object:Handler(){
                override fun handleMessage(msg: Message) {
                    Log.e(TAG,"threadName:${Thread.currentThread().name}")
                    Log.e(TAG,"msg.what:${msg.what}")
                }
            }
            sleep(1)
            handler2?.sendEmptyMessage(0x110)
            Looper.loop()
        }
    }
    inner class MyThread2:Thread(){
        override fun run() {
            Looper.prepare()
            handler2=object:Handler(){
                override fun handleMessage(msg: Message) {
                    Log.e(TAG,"threadName:${Thread.currentThread().name}")
                    Log.e(TAG,"msg.what:${msg.what}")
                }
            }
            sleep(2)
            handler1?.sendEmptyMessage(0x120)
            Looper.loop()
        }
    }



}
